package de.szut.xml;

import de.szut.entity.Country;
import de.szut.entity.OperatingSystem;
import de.szut.entity.OperatingSystems;
import de.szut.entity.User;
import de.szut.util.OperatingSystemName;
import de.szut.util.SecurityLevel;
import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by dzebcuks on 23.08.13.
 */
public class CountryXmlParser extends XmlParser {

    private static final String COUNTRIES = "countries";

    private static final String COUNTRY = "country";

    private static final String NAME = "name";

    private static final String COLOR = "color";

    private static final String SECURITY = "security";

    private static final String OPERATING_SYSTEMS = "operating-systems";

    private static final String USERS = "users";

    private Country country = null;

    @Override
    protected String getRequiredTag() {
        return CountryXmlParser.COUNTRIES;
    }

    @Override
    protected List<String> getNecessaryTags() {
        return new ArrayList<String>(Arrays.asList(CountryXmlParser.COUNTRIES,
                CountryXmlParser.COUNTRY, CountryXmlParser.NAME,
                CountryXmlParser.OPERATING_SYSTEMS, CountryXmlParser.COLOR,
                CountryXmlParser.SECURITY, CountryXmlParser.USERS, OperatingSystemName.LINUX.name
                ().toLowerCase(), OperatingSystemName.MAC.name().toLowerCase(),
                OperatingSystemName.WINDOWS.name().toLowerCase()));
    }

    @Override
    protected void handleTag(List<Object> objects, XmlPullParser parser, String tagName) {
        if (tagName.equals(CountryXmlParser.COUNTRY)) {
            this.country = this.readCountry(parser);
        } else if (tagName.equals(CountryXmlParser.OPERATING_SYSTEMS)) {
            this.country.setOperatingSystems(new OperatingSystems());
        } else if (tagName.equalsIgnoreCase(OperatingSystemName.WINDOWS.name())) {
            this.country.getOperatingSystems().setWindows(new OperatingSystem(OperatingSystemName
                    .WINDOWS, new User(Integer.valueOf(parser.getAttributeValue(null,
                    CountryXmlParser.USERS)))));
        } else if (tagName.equalsIgnoreCase(OperatingSystemName.MAC.name())) {
            this.country.getOperatingSystems().setMac(new OperatingSystem(OperatingSystemName
                    .MAC, new User(Integer.valueOf(parser.getAttributeValue(null,
                    CountryXmlParser.USERS)))));
            if (country != null) { // TODO: workaround -.-
                objects.add(country);
            }
        } else if (tagName.equalsIgnoreCase(OperatingSystemName.LINUX.name())) {
            this.country.getOperatingSystems().setLinux(new OperatingSystem(OperatingSystemName
                    .LINUX, new User(Integer.valueOf(parser.getAttributeValue(null,
                    CountryXmlParser.USERS)))));
        }
    }

    private Country readCountry(XmlPullParser parser) {
        Country country = new Country();
        country.setName(parser.getAttributeValue(null, CountryXmlParser.NAME));
        country.setColor(Integer.valueOf(parser.getAttributeValue(null,
                CountryXmlParser.COLOR)));
        country.setSecurityLevel(SecurityLevel.valueOf(parser.getAttributeValue(null,
                CountryXmlParser.SECURITY)));
        return country;
    }

}
